const {send,pageingQuery} = require("@/zy-common/utils")
const service = require("./index.service")

class indexController {
  async getDynamicList(ctx,next){
    try {
      const {pageIndex = 1,pageSize = 10} = ctx.request.body
      await pageingQuery(service.getDynamicList,service.getDynamicListTotalSize,pageIndex,pageSize,ctx)
    } catch (error) {
      console.log(error);
    }
  }

  async putDynamic(ctx,next){
    try {
      const {title,images,topic_id} = ctx.request.body
      const {id} = ctx.user
      let res = await service.putDynamic(title,images,id,topic_id)
      send(ctx,200,'200','发布动态成功',{id:res.insertId,affectedRows:res.affectedRows})
    } catch (error) {
      console.log(error);
    }
  }

  async putDynamicLikes(ctx,next){
    try {
      const {dynamicId} = ctx.request.body
      const {id} = ctx.user
      console.log(dynamicId,id);
      const res = await service.putDynamicLikes(dynamicId,id)
      if(res?.affectedRows === 1){
        return send(ctx,200,'200','点赞成功',{id:res.insertId,affectedRows:res.affectedRows})
      }
    } catch (error) {
      console.log(error);
      send(ctx,500,'-1','500')
    }
  }

  async deltDynamicLikes(ctx,next){
    try {
      const {dynamicId} = ctx.request.body
      const {id} = ctx.user
      const res = await service.delDynamicLikes(dynamicId,id)
      if(res?.affectedRows === 1){
        return send(ctx,200,'200','取消点赞',{id:res.insertId,affectedRows:res.affectedRows})
      }
      
    } catch (error) {
      console.log(error);
      send(ctx,500,'-1','500')
    }
  }

  async getDynamicLikesList(ctx,next){
    try {
      const {dynamicId} = ctx.request.body
      const res = await service.getDynamicLikesList(dynamicId)
      return send(ctx,200,'200','获取点赞列表成功',res)
    } catch (error) {
      console.log(error);
    }
  }
}

module.exports = new indexController()